package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwaiT;
import cn.e4j.modular.crm.entity.GwailT;
import cn.e4j.modular.crm.entity.GwajT;
import cn.e4j.modular.crm.entity.GwajlT;
import cn.e4j.modular.crm.mapper.GwaiTMapper;
import cn.e4j.modular.crm.service.GwaiTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-10
 */
@Service
public class GwaiTServiceImpl extends ServiceImpl<GwaiTMapper, GwaiT> implements GwaiTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwahDtos = new ArrayList<>();
        if (!dto.getString("GWAM005").equals("1")) {
            gwahDtos = this.baseMapper.listPage(page, dto);
        } else {
            gwahDtos = this.baseMapper.listGwanPage(page, dto);
        }
        return gwahDtos;
    }

    @Override
    public GwaiT getByKey(String GWAI001, String GWAIENT) {
        return this.baseMapper.getByKey(GWAI001, GWAIENT);
    }

    /**
     * 增加
     *
     * @param gwaiT,gwailT
     * @return
     */
    @Transactional
    public void add(GwaiT gwaiT, GwailT gwailT) {
        gwaiT.insert();
        gwailT.insert();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Transactional
    public void edit(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        UpdateWrapper<GwaiT> wrapper = new UpdateWrapper<>();
        GwaiT gwai = new GwaiT();
        wrapper.eq("GWAI001", dto.getString("GWAI001"));
        wrapper.eq("GWAIENT", user.getDBC());
        gwai.setGWAIACTI(dto.getString("GWAIACTI"));
        gwai.setGWAIMODU(user.getACCOUNT());
        UpdateWrapper<GwailT> wrapper1 = new UpdateWrapper<>();
        GwailT gwail = new GwailT();
        gwail.setGWAIL003(dto.getString("GWAIL003"));
        gwail.setGWAIL004(dto.getString("GWAIL004"));
        wrapper1.eq("GWAIL001", dto.getString("GWAI001"));
        wrapper1.eq("GWAILENT", user.getDBC());
        gwai.update(wrapper);
        gwail.update(wrapper1);
    }

    /**
     * 删除
     *
     * @param dto
     * @return
     */
    @Transactional
    public void del(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        UpdateWrapper<GwaiT> wrapper = new UpdateWrapper<>();
        GwaiT gwai = new GwaiT();
        wrapper.eq("GWAI001", dto.getString("GWAI001"));
        wrapper.eq("GWAIENT", user.getDBC());
        UpdateWrapper<GwailT> wrapper1 = new UpdateWrapper<>();
        GwailT gwail = new GwailT();
        wrapper1.eq("GWAIL001", dto.getString("GWAI001"));
        wrapper1.eq("GWAILENT", user.getDBC());
        List<Map<String, Object>> gwajDtos = this.baseMapper.listGwaj(dto);
        if (gwajDtos.size() > 0) {
            UpdateWrapper<GwajT> wrapper2 = new UpdateWrapper<>();
            GwajT gwaj = new GwajT();
            wrapper2.eq("GWAJ001", dto.getString("GWAI001"));
            wrapper2.eq("GWAJENT", user.getDBC());
            UpdateWrapper<GwajlT> wrapper3 = new UpdateWrapper<>();
            GwajlT gwajl = new GwajlT();
            wrapper3.eq("GWAJL001", dto.getString("GWAI001"));
            wrapper3.eq("GWAJLENT", user.getDBC());
            gwaj.delete(wrapper2);
            gwajl.delete(wrapper3);
        }
        gwai.delete(wrapper);
        gwail.delete(wrapper1);
    }

    @Transactional
    public void deleteDataBatch(List<GwaiT> gwaiTList) {
        ShiroUser user = ShiroKit.getUser();
        for (GwaiT gwaiT : gwaiTList) {
            UpdateWrapper<GwaiT> wrapper = new UpdateWrapper<>();
            GwaiT gwai = new GwaiT();
            wrapper.eq("GWAI001", gwaiT.getGWAI001());
            wrapper.eq("GWAIENT", user.getDBC());
            UpdateWrapper<GwailT> wrapper1 = new UpdateWrapper<>();
            GwailT gwail = new GwailT();
            wrapper1.eq("GWAIL001", gwaiT.getGWAI001());
            wrapper1.eq("GWAILENT", user.getDBC());
            Dto dto = Dtos.newDto();
            dto.put("GWAIENT",user.getDBC());
            dto.put("GWAI001",gwaiT.getGWAI001());
            List<Map<String, Object>> gwajDtos = this.baseMapper.listGwaj(dto);
            if (gwajDtos.size() > 0) {
                UpdateWrapper<GwajT> wrapper2 = new UpdateWrapper<>();
                GwajT gwaj = new GwajT();
                wrapper2.eq("GWAJ001", gwaiT.getGWAI001());
                wrapper2.eq("GWAJENT", user.getDBC());
                UpdateWrapper<GwajlT> wrapper3 = new UpdateWrapper<>();
                GwajlT gwajl = new GwajlT();
                wrapper3.eq("GWAJL001", gwaiT.getGWAI001());
                wrapper3.eq("GWAJLENT", user.getDBC());
                gwaj.delete(wrapper2);
                gwajl.delete(wrapper3);
            }
            gwai.delete(wrapper);
            gwail.delete(wrapper1);
        }
    }
}
